<template>
	<view class="page">
		<view class="job-full-head">
			<view class="job-full-back" @click="back()">
				<u-icon name="arrow-left"></u-icon>
			</view>
			<view class="job-full-search">
				<u-search :show-action="false" borderColor="rgb(230, 230, 230)" searchIcon="/static/job/seach1.png"
					bgColor="#eeeeee" height="60" margin="20rpx" shape="square" placeholder="请输入求职关键词"></u-search>
			</view>
			<view class="job-float-navigation" @click="showFloatNavigation()">
				<image src="../../static/job/nav5.png" mode="aspectFit" class="job-float-navigation-image"></image>
			</view>
		</view>
		<!-- <HM-filterDropdown :menuTop="filterDropdownmenuTop" :filterData="filterData" :defaultSelected="defaultSelected"
			:updateMenuName="true" @confirm="confirm" dataFormat="Object"></HM-filterDropdown> -->
			
		<view class="job-filter-dropdown">
			<HM-filterDropdown ref="filterDropdown" :filterData="filterData" :defaultSelected="defaultSelected"
				:updateMenuName="true" @confirm="confirm" dataFormat="Object"></HM-filterDropdown>
		</view>



		<!-- <u-gap height="3" bgColor="#f5f5f5" marginTop="30rpx" marginBottom="0rpx"></u-gap> -->



		<view class="job-full-body">
			<HotJobs :hotJobsList="jobsList"></HotJobs>
			<u-loadmore :status="loadMoreStatus" loadingIcon="circle" loading-text="努力加载中..." loadmore-text="上拉加载" nomore-text="没有更多信息..." />
		</view>
		<view class="job-full-bottom">
			<SecondTabbar @regional="regionalScope"></SecondTabbar>
		</view>

		<!-- 浮动导航开始 -->
		<JobFloatNavigation :jobFloatNavigationIsShow="floatNavigation"></JobFloatNavigation>
		<!-- 浮动导航结束 -->
	</view>
</template>

<script>
	import JobFloatNavigation from '@/components/JobFloatNavigation/JobFloatNavigation.vue';
	import HotJobs from '@/components/HotJobs/HotJobs.vue';
	import SecondTabbar from '@/components/SecondTabbar/SecondTabbar.vue';
	// import data from './data.js'; //筛选菜单数据
	export default {
		data() {
			return {
				thisPage: 1, // 当前默认翻页
				thisAllPage: 1, //当前全部页面
				// filterDropdownmenuTop: 110, // 筛选组件初始距离上边变化距离
				// filterDropdownmenuTopStatic: 110, // 筛选组件初始距离上边初始距离
				loadMoreStatus: 'loading', // 底部加载状态
				floatNavigation: false,
				jobsList: [], // 职位列表
				// indexArr: '',
				// valueArr: '',
				// loadingText: "正在加载...",
				defaultSelected: [], // 筛选默认选择项
				filterData: [], // 组合后的筛选数据
				myAddress: '', // 当前地址
				longitude: '', // 当前经度
				latitude: '', // 当前维度
				coordinate: '', // 经纬度坐标
				defaultCityId: 0 ,// 默认城市ID，默认为不限
				citySelect: [], // 城市选择后传递参数后所选择项目
				filterParameters:{}, // 筛选项目的参数
			};
		},
		filters: {
			// outData(value) {
			// 	return JSON.stringify(value);
			// }
		},
		async onLoad(option) {
			await this.$onLaunched;
			console.log(option)
			
			if(JSON.stringify(option) != "{}") {
				this.defaultCityId = option.cityId
			} else {
				setTimeout(() => {
					//2秒后 修改defaultSelected
					this.changeSelected();
				}, 2000);
			}
			console.log(this.defaultCityId)
			this.readOnLoad() // 加载页面默认数据

			// this.filterData = data; 
			//定时器模拟ajax异步请求数据
			// setTimeout(() => {
			// 	this.filterData = data; 
			// }, 100);

			//模拟ajax请求子菜单数据。
			// setTimeout(() => {
			// 	this.filterData[2].submenu[0].submenu = [{"name": "附近","value": "附近"},{"name": "1km","value": "1km"},{"name": "2km","value": "2km"},{"name": "3km","value": "3km"},{"name": "4km","value": "4km"},{"name": "5km","value": "5km"}];
			// }, 1000)


			// setTimeout(() => {
			// 	//2秒后 修改defaultSelected
			// 	this.changeSelected();
			// }, 2000);
		},
		// 下拉刷新
		onPullDownRefresh(){
			this.$refs.filterDropdown.hideMenu(false)
			this.init()
			this.readOnLoad()
			setTimeout(() => {
				uni.stopPullDownRefresh()
				// console.log('停止下拉刷新')
			}, 1000);
			// uni.showToast({
			// 	title: '触发下拉刷新'
			// });
			
		},
		//上拉加载，需要自己在page.json文件中配置"onReachBottomDistance"
		onReachBottom() {
			this.loadMoreStatus = 'loading'
			if(this.thisPage < this.thisAllPage){
				this.thisPage++
				console.log(this.filterParameters)
				if(Object.keys(this.filterParameters).length == 0) {
					this.getJobRecruitList()
				} else {
					this.getJobRecruitScreenList()
				}
			} else {
				this.loadMoreStatus = 'nomore'
			}
			
			// uni.showToast({
			// 	title: '触发上拉加载'
			// });
		
			// let len = this.goodsList.length;
			// if (len >= 80) {
			// 	this.loadingText = "到底了";
			// 	return false;
			// } else {
			// 	this.loadingText = "正在加载...";
			// }
		
		},
		onPageScroll(e) {
			// console.log(e.scrollTop)
		},
		methods: {
			// 初始化页面数据
			init() {
				// this.filterData = [] // 组合后的筛选数据
				this.thisPage = 1  // 当前默认翻页
				this.thisAllPage = 1  //当前全部页面
				this.floatNavigation = false // 顶部浮动菜单隐藏
				this.jobsList = []  // 职位列表
			},
			readOnLoad() {
				this.getJobRecruitList()
				this.getLocation() // 获取经纬度
			},
			showFloatNavigation() {
				this.floatNavigation = !this.floatNavigation;
			},
			// 返回上一页面
			back(){
				uni.$u.route({
					type:'navigateBack',
					delta: 1,
				})
			},
			// 获取地址及坐标
			async getLocation() {
				this.isShowAddressPopup = false
				await uni.getLocation({
					type: 'gcj02',
					geocode: true,
					success: res => {
						this.longitude = res.longitude;
						this.latitude = res.latitude;
						this.coordinate = this.latitude + ',' + this.longitude;
						this.getLocationAddress(this.latitude, this.longitude);
						console.log(res);
					}
				});
			},
			// 获取地址
			async getLocationAddress(longitude, latitude) {
				let location = [longitude, latitude];
				let StringLocation = location.toString();
				// console.log(StringLocation)
				await this.$api.getLocationAddress({
					get_poi: 1,
					key: this.$qqMapKey,
					location: StringLocation
				}).then(res => {
					// console.log(res)
					this.myAddress = res.result.formatted_addresses.recommend;
				});
			},
			// 获取页面默认接口数据
			getJobRecruitList() {
				this.$api.getJobRecruitList({
					openid: getApp().globalData.openid,
					page: this.thisPage
				}).then(res => {
					// console.log(res.data)
					if(this.defaultCityId) {
						this.getJobRecruitScreenList({CityID: this.defaultCityId})
					} else {
						// this.jobsList = res.data.XXList
						this.jobsList = [...this.jobsList, ...res.data.XXList]
						this.$forceUpdate();
					}
					this.thisPage = res.data.CurrentPage
					this.thisAllPage = res.data.Allpage
					// if(!this.jobsList.length) {
						this.loadMoreStatus = 'loadmore'
					// }
					if(this.filterData.length == 0) {
						// this.filterData = []
						// 组合地区选项
						let filterDataArea = {
							"name": "地区",
							"type": "hierarchy-column",
							"submenu": [{
								"name": "不限",
								"value": 0,
								"submenu": [{
									"name": "不限城市",
									"value": 0,
								}]
							}],
						}
						// console.log(res.data.DiYuList)
						for (let [indexCity, areaItem] of res.data.DiYuList.entries()) {
							
							let areaItemCity = {
								"name": areaItem.Name,
								"value": areaItem.ID,
								"submenu": [{
									// "name": areaItem.Name + "不限区",
									"name": "不限区",
									"value": 0,
								}]
							}
							// 传递城市ID的选择数组，需要修改默认筛选选项
							if(this.defaultCityId == areaItem.ID) {
								this.citySelect = [indexCity+1, 0]
								setTimeout(() => {
									//2秒后 修改defaultSelected
									this.changeSelected();
								}, 2000);
							}
							// console.log(areaItemCity)
							for (let cityItem of areaItem.ddtype_lst) {
								let areaItemArea = {
									"name": cityItem.dd_name,
									"value": cityItem.dd_id,
								}
								// console.log(areaItemArea)
								areaItemCity.submenu.push(areaItemArea)
							}
							filterDataArea.submenu.push(areaItemCity)

						}
						// console.log(filterDataArea)
						this.filterData.push(filterDataArea)

						// 组合职位选项
						let filterDataJob = {
							"name": "职位",
							"type": "hierarchy-column",
							"submenu": [{
								"name": "不限",
								"value": 0,
								"submenu": [{
									"name": "不限行业",
									"value": 0,
								}]
							}],
						}
						for (let jobItem of res.data.ZhiWeiList) {
							let jobItemIndustry = {
								"name": jobItem.ClassName,
								"value": jobItem.ID,
								"submenu": [{
									"name": "不限职位",
									"value": 0,
								}]
							}
							// console.log(jobItemIndustry)
							for (let positionItem of jobItem.zwtype_lst) {
								let jobItemPosition = {
									"name": positionItem.zw_name,
									"value": positionItem.zw_id,
								}
								// console.log(jobItemPosition)
								jobItemIndustry.submenu.push(jobItemPosition)
							}
							filterDataJob.submenu.push(jobItemIndustry)

						}
						// console.log(filterDataJob)
						this.filterData.push(filterDataJob)
						// 组合职位要求
						let filterDataJobRequire = {
							"name": "职位要求",
							"type": "radio",
							"submenu": [],
						}
						// 组合学历
						let filterDataJobeducation = {
							"name": "学历",
							"value": 0,
							"submenu": [{
								"name": "不限",
								"value": 0,
							}]
						}

						for (let educationItem of res.data.XLList) {
							let jobItemEducation = {
								"name": educationItem.XLName,
								"value": educationItem.ID,
							}
							// console.log(jobItemPosition)
							filterDataJobeducation.submenu.push(jobItemEducation)
						}
						filterDataJobRequire.submenu.push(filterDataJobeducation)


						// 组合薪资
						let filterDataJobSalary = {
							"name": "薪资",
							"value": 0,
							"submenu": [{
								"name": "不限",
								"value": 0,
							}]
						}

						for (let salaryItem of res.data.XZList) {
							let jobItemSalary = {
								"name": salaryItem.XZName,
								"value": salaryItem.ID,
							}
							// console.log(jobItemPosition)
							filterDataJobSalary.submenu.push(jobItemSalary)
						}
						filterDataJobRequire.submenu.push(filterDataJobSalary)

						// 组合经验
						let filterDataJobExperience = {
							"name": "经验",
							"value": 0,
							"submenu": [{
								"name": "不限",
								"value": 0,
							}]
						}

						for (let experienceItem of res.data.GZjyList) {
							let jobItemExperience = {
								"name": experienceItem.JYName,
								"value": experienceItem.ID,
							}
							// console.log(jobItemPosition)
							filterDataJobExperience.submenu.push(jobItemExperience)
						}
						filterDataJobRequire.submenu.push(filterDataJobExperience)

						// 刷新时间
						let filterDataJobRefreshTime = {
							"name": "刷新时间",
							"value": 0,
							"submenu": [{
									"name": "不限",
									"value": 0,
								},
								{
									"name": "一天内",
									"value": 1,
								},
								{
									"name": "一周内",
									"value": 2,
								},
								{
									"name": "两周内",
									"value": 3,
								},
								{
									"name": "一月内",
									"value": 4,
								}
							]
						}
						filterDataJobRequire.submenu.push(filterDataJobRefreshTime)
						// 加入到筛选中
						this.filterData.push(filterDataJobRequire)

						// 组合公司要求
						let filterDataCompany = {
							"name": "公司要求",
							"type": "radio",
							"submenu": [],
						}

						// 组合规模
						let filterDataCompanySize = {
							"name": "公司规模",
							"value": 0,
							"submenu": [{
								"name": "不限",
								"value": 0,
							}]
						}

						for (let companySizeItem of res.data.GMList) {
							let jobItemCompanySize = {
								"name": companySizeItem.Name,
								"value": companySizeItem.ID,
							}
							filterDataCompanySize.submenu.push(jobItemCompanySize)
						}
						filterDataCompany.submenu.push(filterDataCompanySize)

						// 组合公司性质
						let filterDataCompanyNature = {
							"name": "公司性质",
							"value": 0,
							"submenu": [{
								"name": "不限",
								"value": 0,
							}]
						}

						for (let companyNatureItem of res.data.XingZhi) {
							let jobItemCompanyNature = {
								"name": companyNatureItem.Name,
								"value": companyNatureItem.ID,
							}
							filterDataCompanyNature.submenu.push(jobItemCompanyNature)
						}
						filterDataCompany.submenu.push(filterDataCompanyNature)

						// 组合认证企业
						let filterDataCertifiedEnterprise = {
							"name": "认证企业",
							"value": 0,
							"submenu": [{
									"name": "不限",
									"value": 0,
								},
								{
									"name": "认证企业",
									"value": 1,
								}
							]
						}
						filterDataCompany.submenu.push(filterDataCertifiedEnterprise)
						// 加入到筛选中
						this.filterData.push(filterDataCompany)
					}

				})
			},
			
			// 获取页面筛选接口数据
			getJobRecruitScreenList(option) {
				// 组合筛选参数
				this.loadMoreStatus = 'loading'
				let screenList = {openid: getApp().globalData.openid, ProID: 1,page: this.thisPage, ...this.filterParameters, ...option}
				this.$api.getJobRecruitScreenList(screenList).then(res => {
					// console.log(res.data)
					this.jobsList = [...this.jobsList, ...res.data.XXList]
					this.thisPage = res.data.CurrentPage
					this.thisAllPage = res.data.Allpage
					// if(!this.jobsList.length) {
						this.loadMoreStatus = 'loadmore'
					// }
				})
			},
			//接收菜单结果
			confirm(e) {
				// console.log(e)
				let tempFilterParameters = {}
				this.indexArr = e.index;
				this.valueArr = e.value;
				// console.log(this.filterData)
				// console.log(e.value)
				for(let [indexItem, searchItem] of e.value.entries()) {
					// console.log(indexItem, searchItem)
					// indexItem 为0时组合地区
					if(indexItem == 0) {
						if(searchItem[0] != 0) {
							if(searchItem[1] != 0) {
								tempFilterParameters = Object.assign(tempFilterParameters, {CityID: searchItem[1]})
							} else {
								tempFilterParameters = Object.assign(tempFilterParameters, {CityID: searchItem[0]})
							}
							
						}
						continue
					}
					// indexItem 为1时组合职位
					if(indexItem == 1) {
						if(searchItem[0] != 0) {
							if(searchItem[1] != 0) {
								tempFilterParameters = Object.assign(tempFilterParameters, {ZWID1: searchItem[0]}, {ZWID2: searchItem[1]})
							} else {
								tempFilterParameters = Object.assign(tempFilterParameters, {ZWID1: searchItem[0]})
							}
						}
						continue
					}
					// indexItem 为2时组合职位要求
					if(indexItem == 2) {
						for(let [indexRequired, requiredItem] of searchItem.entries()) {
							// indexRequired 为 0 时 组合学历
							if(indexRequired == 0) {
								if(requiredItem[0] != 0) {
									tempFilterParameters = Object.assign(tempFilterParameters, {XuLiID: requiredItem[0]})
								}
								continue
							}
							
							// indexRequired 为 1 时 组合薪资
							if(indexRequired == 1) {
								if(requiredItem[0] != 0) {
									tempFilterParameters = Object.assign(tempFilterParameters, {XZID: requiredItem[0]})
								}
								continue
							}
							
							// indexRequired 为 2 时 组合经验
							if(indexRequired == 2) {
								if(requiredItem[0] != 0) {
									tempFilterParameters = Object.assign(tempFilterParameters, {GZJYID: requiredItem[0]})
								}
								continue
							}
							
							// indexRequired 为 3 时 组合刷新时间
							if(indexRequired == 3) {
								if(requiredItem[0] != 0) {
									tempFilterParameters = Object.assign(tempFilterParameters, {SXDate: requiredItem[0]})
								}
								continue
							}
						}
						continue
					}
					// indexItem 为3时组合公司要求
					if(indexItem == 3) {
						for(let [indexCompany, companyItem] of searchItem.entries()) {
							// indexRequired 为 0 时组合公司规模
							if(indexCompany == 0) {
								if(companyItem[0] != 0) {
									tempFilterParameters = Object.assign(tempFilterParameters, {GMID: companyItem[0]})
								}
								continue
							}
							
							// indexRequired 为 1 时 组合公司性质
							if(indexCompany == 1) {
								if(companyItem[0] != 0) {
									tempFilterParameters = Object.assign(tempFilterParameters, {XingZhiID: companyItem[0]})
								}
								continue
							}
							
							// indexRequired 为 2 时 组合认证企业
							if(indexCompany == 2) {
								if(companyItem[0] != 0) {
									tempFilterParameters = Object.assign(tempFilterParameters, {IsRZ: companyItem[0]})
								}
								continue
							}
						continue
						}
					}
				}
				this.filterParameters = tempFilterParameters
				this.jobsList = []
				this.thisPage = 1				this.thisAllPage = 1
				this.getJobRecruitScreenList()
				// console.log(tempFilterParameters)
				// console.log(this.filterParameters)
			},
			//修改选中项-示例
			changeSelected() {
				//设置选中项
				// 一下的注释是针对测试数据说明结构的意思，具体传入什么数据，要看你自己数据。如果data.js数据有修改，注意defaultSelected也要修改
				//传入defaultSelected的结构不能错，错了就报错运行异常。 不想选中的请传入null

				this.defaultSelected = [];
				if(this.citySelect.length) {
					this.$nextTick(() => {
						this.defaultSelected = [
							this.citySelect, // this.citySelect是一个数组//第0个菜单选中 一级菜单的第6项，二级菜单的第0项，三级菜单的第1项
							[0, 0], //第1个菜单选中 一级菜单的第2项，二级菜单的第0项，三级菜单的第1项
							[
								[0],
								[0],
								[0],
								[0]
							], //第2个菜单选中 一级菜单的第1项 一级菜单的第1项
							// [
							// 	[0],
							// 	[1, 2, 7],
							// 	[1, 0]
							// ], //筛选菜单选中 第一个筛选的第0项，第二个筛选的第1,2,7项，第三个筛选的第1,0项
							[
								[0],
								[0],
								[0]
							] //单选菜单选中 第一个筛选的第0项，第二个筛选的第1项，第三个筛选的第1项
						];
					})
				} else {
					this.$nextTick(() => {
						this.defaultSelected = [
							[0, 0], //第0个菜单选中 一级菜单的第6项，二级菜单的第0项，三级菜单的第1项
							[0, 0], //第1个菜单选中 一级菜单的第2项，二级菜单的第0项，三级菜单的第1项
							[
								[0],
								[0],
								[0],
								[0]
							], //第2个菜单选中 一级菜单的第1项 一级菜单的第1项
							// [
							// 	[0],
							// 	[1, 2, 7],
							// 	[1, 0]
							// ], //筛选菜单选中 第一个筛选的第0项，第二个筛选的第1,2,7项，第三个筛选的第1,0项
							[
								[0],
								[0],
								[0]
							] //单选菜单选中 第一个筛选的第0项，第二个筛选的第1项，第三个筛选的第1项
						];
					})
				}
				
			},
			//轮播图指示器
			swiperChange(event) {
				//this.currentSwiper = event.detail.current;
			},
			// 附近职位列表信息
			getJobRecruitNearbyList(){
				this.loadMoreStatus = 'loading'
				let screenList = {openid: getApp().globalData.openid, ProID: 1,page: this.thisPage, zuobiao: this.coordinate }
				this.$api.getJobRecruitNearbyList(screenList).then(res => {
					// console.log(res.data)
					this.jobsList = [...this.jobsList, ...res.data.XXList]
					this.thisPage = res.data.CurrentPage
					this.thisAllPage = res.data.Allpage
					if(!this.jobsList.length) {
						this.loadMoreStatus = 'nomore'
					}
				})
			},
			// 切换地域范围
			regionalScope(e){
				console.log(e)
				this.init()
				if(e == 'all') {
					this.getJobRecruitList()
				} else {
					this.getJobRecruitNearbyList()
				}
			}
		},
		
		components: {
			JobFloatNavigation,
			HotJobs,
			SecondTabbar,
		}
	}
</script>

<style lang="scss" scoped>
	@import url('jobRecruit.css');
</style>
